package com.dotstone.chipism.ui.timing.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;
import android.util.Log;


/**
 * 
 */
public class ShareParam {
	
	private static final String SHAREDPREFERENCES_NAME = "DotStone_Chipism";
	 private static final String SAVETAG = "list"; 

	static public void putStringParam(Context context ,String key,String value)
	{
		SharedPreferences ffchat = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); 
		 Editor ffchatedit = ffchat.edit(); 
		 ffchatedit.putString(key, value);
		 ffchatedit.commit();
	}
	static public void putIntParam(Context context ,String key,int value)
	{
		SharedPreferences ffchat = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); 
		 Editor ffchatedit = ffchat.edit(); 
		 ffchatedit.putInt(key, value);
		 ffchatedit.commit();
	}
	static public void putBooleanParam(Context context ,String key,boolean value)
	{
		SharedPreferences ffchat = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); 
		 Editor ffchatedit = ffchat.edit(); 
		 ffchatedit.putBoolean(key, value);
		 ffchatedit.commit();
	}
	
	
	static public String getStringParam(Context context ,String key)
	{
		SharedPreferences ffchat = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); 
		
		String result = ffchat.getString(key, "");
		return result;
				
	}
	static public int getIntParam(Context context ,String key)
	{
		SharedPreferences ffchat = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); 
		
		int result = ffchat.getInt(key, 1);
		return result;
				
	}
	static public boolean getBooleanParam(Context context ,String key)
	{
		SharedPreferences ffchat = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); 
		boolean b=ffchat.getBoolean(key, true);
		return b;	
	}
	
	static public void deleteParamValue(Context context ,String key)
	{
		SharedPreferences ffchat = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); 
		Editor ffchatedit = ffchat.edit(); 
		ffchatedit.remove(key);
		ffchatedit.commit();
	}
	
	/** 
   
     */  
    public static void saveObject(Object object,Context context,String shaPreName){  
        SharedPreferences sharedPreferences =  
                context.getSharedPreferences(shaPreName, Activity.MODE_PRIVATE);  
        Editor editor = sharedPreferences.edit();
        List<Object> list = getObject(context,shaPreName);  
        if(null == list){  
            list = new ArrayList<Object>();  
        }  
        list.add(object);  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        try {  
            ObjectOutputStream oos = new ObjectOutputStream(baos);  
            oos.writeObject(list);  
            String strList = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));  
            editor.putString(SAVETAG, strList);  
            editor.commit();  
            oos.close();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }finally{  
            try {  
                baos.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
    }  
  
    /** 
     
     * @param context 
     * @param shaPreName    
     * @return 
     */  
    public static List<Object> getObjects(Context context,String shaPreName){  
        List<Object> list;  
        SharedPreferences sharedPreferences =  
                context.getSharedPreferences(shaPreName, Activity.MODE_PRIVATE);  
        String message  = sharedPreferences.getString(SAVETAG, "");  
        byte[] buffer = Base64.decode(message.getBytes(), Base64.DEFAULT);  
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);  
        try {  
            ObjectInputStream ois = new ObjectInputStream(bais);  
            list = (List<Object>)ois.readObject();  
            ois.close();  
            return list;  
        } catch (StreamCorruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (ClassNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }finally{  
            try {  
                bais.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
        return null;  
    }  
    
    
    
    /**
     * 针对复杂类型存储<对象>
     *
     * @param key
     * @param val
     */
    public static void putObject(Context context,String key, Object object) {
        SharedPreferences sp = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {

            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            Editor editor = sp.edit();
            editor.putString(key, objectVal);
            editor.commit();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static <T> T getObject(Context context,String key) {
        SharedPreferences sp = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        if (sp.contains(key)) {
            String objectVal = sp.getString(key, null);
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                @SuppressWarnings("unchecked")
				T t = (T) ois.readObject();
                return t;
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
	

}
